package de.jare.photoswapper;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ImageView;


// http://en.androidwiki.com/wiki/Loading_images_from_a_remote_server
// erstmla nur _ein_ statisches bild...

public class Get extends Activity {
     private ImageView imView;
     private Bitmap bmImg;
     private String imageUrl = "http://swap.ganzbunt.de/";
     private ProgressDialog pdlg = null;
     private HttpHandler h = new HttpHandler();
      
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.get);
        
        imView = (ImageView)findViewById(R.id.imview);     
        
        //downloadFile();
        byte[] image = h.ReceivePicture();
        
        Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
        imView.setImageBitmap(bmp);
        
 
    }
    
    @Override
    public void onConfigurationChanged(Configuration _newConfig) {
    	super.onConfigurationChanged(_newConfig);
    
    	if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    		//
    	}
    	if (_newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
    		//
    	}
    	if (_newConfig.orientation == Configuration.ORIENTATION_SQUARE) {
    		//
    	}
    	if (_newConfig.orientation == Configuration.ORIENTATION_UNDEFINED) {
    		//
    	}
    
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.get_menu, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.quit:
            Quit();
            return true;
        case R.id.about:
        	startAboutActivity();	
        	break;
        case R.id.register:
        	startRegisterActivity();
        	break;
        case R.id.nw:
        	startPreviewActivity();
        	break;
        }
        return false;
    }
    
    public void startAboutActivity() {
		Intent i = new Intent(this, About.class);
		startActivity(i);
    }
    
    public void startPreviewActivity() {
    	Intent i = new Intent(this, Preview.class);
		startActivity(i);
		finish();
    }
    
    public void startRegisterActivity() {
    	Intent i = new Intent(this, Register.class);
    	startActivity(i);
    }
    
    public void Quit() {
    	finish();
    }

    private void downloadFile(){
    	
    	pdlg = ProgressDialog.show(this, "Receiving picture", "Wait a second...", false, true);
        
    	new Thread() {
    		public void run () {
	    		URL myFileUrl = null;
	    		try {
					myFileUrl = new URL("http://swap.ganzbunt.de/get.php?AndroidId=" + HttpHandler.AndroidID + "&IMEI=" + HttpHandler.DeviceID + "&MobileNr=" + HttpHandler.MobileNr);
				} catch (MalformedURLException e1) {
					e1.printStackTrace();
				}
	    		try {
	                HttpURLConnection conn = (HttpURLConnection)myFileUrl.openConnection();
	                conn.setDoInput(true);
	                conn.connect();
	                InputStream is = conn.getInputStream();
	                bmImg = BitmapFactory.decodeStream(is);
	                handler.sendEmptyMessage(0);
	                
	    		} catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	           	}
    		}
    	}.start();
     }
    
    private Handler handler = new Handler() {
    	public void handleMessage(Message ms) {
    		imView.setImageBitmap(bmImg);
    		pdlg.dismiss();
    	}
    };
    	
    	    	
    
}